import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import mitter from '@/mitt'
import {
  apiCollectMind,
  apiUnCollectMind,
  apiTrashFolder,
  apiDeleteFolder,
  apiTrashMind,
  apiRecoverTrashFolder,
  apiRecoverTrashMind,
  apiDeleteMind
} from '@/apis/work'
import type { Folder, Mind } from '@/types'
import NewFolderMindModal from '@/pages/work/components/NewFolderMind.vue'
import MoveToFolder from '@/pages/work/components/MoveToFolder.vue'
import ShareMindModal from '@/pages/work/components/ShareMindModal.vue'

export default function useViewCardList (
  emits: (
    event: 'refresh-page' | 'update-collect-status' | 'folder-click' | 'mind-click',
    ...args: any[]
  ) => void,
  type = 'work'
) {
  // 文件夹操作菜单集合
  const FOLDER_OPERATE_MENUS = type === 'work' ? [
    {
      name: '重命名',
      icon: 'ri-edit-line',
      command: 'rename'
    },
    {
      name: '移动到',
      icon: 'ri-corner-up-right-line',
      command: 'move'
    },
    {
      name: '移至回收站',
      icon: 'ri-delete-bin-line',
      command: 'trash'
    }
  ] : [
    {
      name: '恢复',
      icon: 'ri-arrow-go-back-line',
      command: 'recover'
    },
    {
      name: '删除',
      icon: 'ri-delete-bin-line',
      command: 'delete'
    }
  ]

  // 导图操作菜单集合
  const MIND_OPERATE_MENUS = type === 'work' ? [
    {
      name: '重命名',
      icon: 'ri-edit-line',
      command: 'rename'
    },
    {
      name: '分享',
      icon: 'ri-share-line',
      command: 'share'
    },
    {
      name: '移动到',
      icon: 'ri-corner-up-right-line',
      command: 'move'
    },
    {
      name: '移至回收站',
      icon: 'ri-delete-bin-line',
      command: 'trash'
    }
  ] : [
    {
      name: '恢复',
      icon: 'ri-arrow-go-back-line',
      command: 'recover'
    },
    {
      name: '删除',
      icon: 'ri-delete-bin-line',
      command: 'delete'
    }
  ]

  const operateType = ref<'folder' | 'mind'>('folder')
  const folderDetail = ref<null | Folder>(null)
  const mindDetail = ref<null | Mind>(null)
  const folderRenameModalVisible = ref(false)
  const shareModalVisible = ref(false)
  const moveToFolderModalVisible = ref(false)

  /**
   * 文件夹操作
   * @param cmd
   * @param folder
   */
  const handleOperateFolder = (cmd: string, folder: Folder) => {
    operateType.value = 'folder'
    mindDetail.value = null
    folderDetail.value = folder
    switch (cmd) {
      case 'rename':
        showFolderMindRenameModal()
        break
      case 'trash':
        handleTrashFolder(folder.id)
        break
      case 'move':
        showMoveToFolderModal()
        break
      case 'recover':
        handleRecoverTrashFolder(folder.id)
        break
      case 'delete':
        handleDeleteFolder(folder.id)
        break
      default:
        break
    }
  }

  /**
   * 文件夹移至回收站
   * @param id
   */
  const handleTrashFolder = async (id: string) => {
    await apiTrashFolder({ id })
    ElMessage.success('操作成功')
    emits('refresh-page')
  }

  /**
   * 文件夹从回收站移出
   * @param id
   */
  const handleRecoverTrashFolder = async (id: string) => {
    await apiRecoverTrashFolder({ id })
    ElMessage.success('操作成功')
    emits('refresh-page')
  }

  /**
   * 删除文件夹
   * @param id
   */
  const handleDeleteFolder = async (id: string) => {
    await ElMessageBox.confirm(
      '删除文件夹会同时删除该文件夹下的所有子目录和导图，并且删除后不可撤销！确认删除？',
      '删除提示',
      {
        confirmButtonText: '删除',
        cancelButtonText: '我再想想',
        type: 'warning',
        showClose: false
      }
    )
    await apiDeleteFolder({ id })
    ElMessage.success('操作成功')
    emits('refresh-page')
  }

  /**
   * 导图操作
   * @param cmd
   * @param mind
   */
  const handleOperateMind = (cmd: string, mind: Mind) => {
    operateType.value = 'mind'
    folderDetail.value = null
    mindDetail.value = mind
    switch (cmd) {
      case 'rename':
        showFolderMindRenameModal()
        break
      case 'share':
        showShareModal()
        break
      case 'trash':
        handleTrashMind([mind.id])
        break
      case 'move':
        showMoveToFolderModal()
        break
      case 'recover':
        handleRecoverTrashMind(mind.id)
        break
      case 'delete':
        handleDeleteMind([mind.id])
        break
      default:
        break
    }
  }

  /**
   * 导图移至回收站
   * @param id
   */
  const handleTrashMind = async (ids: string[]) => {
    await apiTrashMind({ ids })
    ElMessage.success('操作成功')
    emits('refresh-page')
    mitter.emit('refresh-collect-list')
  }

  /**
   * 导图从回收站移出
   * @param id
   */
  const handleRecoverTrashMind = async (id: string) => {
    await apiRecoverTrashMind({ id })
    ElMessage.success('操作成功')
    emits('refresh-page')
  }

  /**
   * 删除文件夹
   * @param id
   */
  const handleDeleteMind = async (ids: string[]) => {
    await ElMessageBox.confirm(
      '删除导图后不可撤销！确认删除？',
      '删除提示',
      {
        confirmButtonText: '删除',
        cancelButtonText: '我再想想',
        type: 'warning',
        showClose: false
      }
    )
    await apiDeleteMind({ ids })
    ElMessage.success('操作成功')
    emits('refresh-page')
  }

  /**
   * 切换导图收藏状态
   * @param id
   * @param status
   */
  const updateMindCollectState = async (id: string, status: boolean) => {
    const API = status ? apiCollectMind : apiUnCollectMind
    await API(id)
    emits('update-collect-status', id, status)
    ElMessage.success('操作成功')
    mitter.emit('refresh-collect-list')
  }

  /**
   * 显示文件夹/导图重命名弹窗
   */
  const showFolderMindRenameModal = () => {
    folderRenameModalVisible.value = true
  }

  /**
   * 分享弹窗显示
   */
  const showShareModal = () => {
    shareModalVisible.value = true
  }

  /**
   * 显示移动文件夹目录弹窗
   */
  const showMoveToFolderModal = () => {
    moveToFolderModalVisible.value = true
  }

  const handleCloseModal = (refresh = false) => {
    folderRenameModalVisible.value = false
    moveToFolderModalVisible.value = false
    shareModalVisible.value = false
    if (refresh) {
      emits('refresh-page')
      mitter.emit('refresh-collect-list')
    }
  }

  /**
   * 文件夹点击
   * @param folterItem
   */
  const handleFolderClick = (folterItem: Folder) => {
    emits('folder-click', folterItem)
  }

  /**
   * 导图点击
   */
  const handleMindClick = (mindItem: Mind) => {
    emits('mind-click', mindItem)
  }

  return {
    FOLDER_OPERATE_MENUS,
    MIND_OPERATE_MENUS,
    folderRenameModalVisible,
    shareModalVisible,
    moveToFolderModalVisible,
    folderDetail,
    mindDetail,
    operateType,
    handleFolderClick,
    handleMindClick,
    handleOperateFolder,
    handleOperateMind,
    handleCloseModal,
    updateMindCollectState,
    handleTrashMind,
    handleDeleteMind,
    NewFolderMindModal,
    MoveToFolder,
    ShareMindModal
  }
}
